package com.zealcomm.ivcscustomer.service

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.zealcomm.commonui.base.BaseFragment
import com.zco.ivcs.customer.base.entity.BusinessScenariosBean
import com.zealcomm.ivcscustomer.R
import com.zealcomm.ivcscustomer.databinding.FragmentCustomerTypeListBinding

/**
 * 服务场景选择
 */
class CustomerTypeListFragment : BaseFragment{

    private constructor(){}

    constructor(businessScenarioList : List<BusinessScenariosBean.BusinessScenario>?){
        this.businessScenarioList.clear()
        businessScenarioList?.let{
            this.businessScenarioList.addAll(it)
        }
    }

    private var binding: FragmentCustomerTypeListBinding? = null
    private var businessScenarioList = arrayListOf<BusinessScenariosBean.BusinessScenario>()
    private var mServiceListAdapter: ServiceListAdapter? = null

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentCustomerTypeListBinding.inflate(inflater, container, false)
        binding!!.tvNone.visibility = if (this.businessScenarioList.size == 0) View.VISIBLE else View.GONE
        return binding?.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mServiceListAdapter = ServiceListAdapter(R.layout.service_item, businessScenarioList)
        binding?.rvSkillList?.adapter = mServiceListAdapter
        mServiceListAdapter!!.setOnItemClickListener { _, _, position ->
            if (holdingActivity is MainActivity) {
                businessScenarioList.forEach {
                    it.isSelected = false
                }
                businessScenarioList[position].isSelected = true
                mServiceListAdapter!!.notifyDataSetChanged()
                (holdingActivity as MainActivity).removeCustomerTypeList(businessScenarioList[position])
            }
        }
    }

    override fun initView(view: View?, savedInstanceState: Bundle?) {
    }

    override val layoutId: Int
        get() = R.layout.fragment_customer_type_list

    companion object {
        @JvmStatic
        fun newInstance(businessScenarioList: ArrayList<BusinessScenariosBean.BusinessScenario>?) =
            CustomerTypeListFragment(businessScenarioList)
    }
}